'use strict';
app.controller('printerTemplateAddCtrl', ['$scope','$state','toaster','$modal', '$log','$timeout','printerTemplateAddService', function($scope,$state,toaster,$modal,$log,$timeout,printerTemplateAddService) {
    $scope.printerTemplateAddVO={content:'',code:[]};

    //查询品牌信息
    $scope.addPrinterTemplate = function () {
        compose();
        printerTemplateAddService.addPrinterTemplate($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $state.go("app.printerTemplate");
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    }

    var compose=function(){
        $scope.printerTemplateAddVO.code=[];
        $scope.printerTemplateAddVO.content="";
        var prev='<VN>0</VN><MN>multiCouplet</MN>\r <center><FS>**#@@2dailyOrderNumber 乐鸽餐饮 type **</FS></center>\r<FH><center>shopName</center></FH>\r********************************\r';
        var last="<center>谢谢惠顾,欢迎下次光临</center><center>乐鸽技术支持</center><center>400-931-8788</center>";
        $scope.printerTemplateAddVO.content+=prev;
        for(var i=0;i<$scope.data.length;i++){
            $scope.printerTemplateAddVO.content+=$scope.data[i].templateContent;
            $scope.printerTemplateAddVO.code.push($scope.data[i].code);
        }

        $scope.printerTemplateAddVO.content+=last;
    }


    //拖拽排版
    $scope.data = [
        {code:6,select:false,content:"桌号:5号",templateContent:'桌号:@@2code \r'},
        {code:1,select:false,content:"店铺订单编号:44578797879789</br>支付方式:微信支付</br>交易时间:2018-06-10 12:00:00",templateContent:'店铺订单编号:shopOrderNumber \r支付方式:payWay \r支付时间:payTime \r'},
        {code:0,select:false,content:"***********************************</br>",templateContent:"<center>********************************</center>"},
        {code:2,select:false,content:"<table class='table font-bold h6'><tr><td>商品详情</td><td>数量</td><td>金额(元)</td></tr><tr><td>剁椒鱼头</td><td>5</td><td>2.00</td></tr><tr><td>东坡肘子</td><td>1</td><td>25.25</td></tr></table>",templateContent:'<table><tr><td>商品详情</td><td>数量</td><td>金额(元)</td></tr></table>\r'},
        {code:0,select:false,content:"***********************************</br>",templateContent:"<center>********************************</center>"},
        {code:3,select:false,content:"订单原价:260.88 元</br>优惠金额:10.00元</br>会员卡折扣:10.00元</br>实付:<b class='font-bold h4'>250.88元</b>",templateContent:"订单原价:realMoney元 \r优惠金额:couponsMoney元\r会员卡折扣:privilegeMoney元 \r实付:@@2<FS><FB>payMoney元</FB></FS>\r"},
        {code:0,select:false,content:"***********************************</br>",templateContent:"<center>********************************</center>"},
        {code:4,select:false,content:"买家留言:</br>用餐餐具:25人,口味:鸳鸯火锅",templateContent:"买家留言:\rremark \r"},
        {code:0,select:false,content:"***********************************</br>",templateContent:"<center>********************************</center>"},
        {code:5,select:false,content:"<div class='inline w-full m-n'><p class='pull-left m-n'>小明</p><p class='pull-right m-n'>13100000000</p></div></br>地址:兰埔富华里金融科技中心",templateContent:"<LR>userName,userPhone</LR>\raddress\r"},
        {code:0,select:false,content:"***********************************</br>",templateContent:"<center>********************************</center>"},
    ];

    $scope.sortableOptions = {
        // 数据有变化
        update: function(e, ui) {
            console.log("update");
            //需要使用延时方法，否则会输出原始数据的顺序，可能是BUG？
            $timeout(function() {
                var resArr = [];
                for (var i = 0; i < $scope.data.length; i++) {
                    resArr.push($scope.data[i]);
                }
            })


        },
        // 完成拖拽动作
        stop: function(e, ui) {
            //do nothing
        }
    }

    //选择块域
    $scope.changeModules=function(item){
        for(var i=0;i<$scope.data.length;i++){
            $scope.data[i].addStatus=false;
        }

        for(var i=0;i<$scope.data.length;i++){
            if($scope.data[i]!=item){
                $scope.data[i].select=false;
            }
        }
        item.select=!item.select;
    }

    //删除块域
    $scope.deleteModules=function(item){
        var index=$scope.data.indexOf(item);
        if(index==-1){
            return;
        }
        $scope.data.splice(index,1);
    }

    var regexpParse={
        parseHtml:function(tag,prev,last,content){
            var prevRegExp = new RegExp("<"+tag+">","g");
            var lastRegExp = new RegExp("<\/"+tag+">","g");
            content=content.replace(prevRegExp,prev).replace(lastRegExp,last);
            return content;
        },
        parseBr:function(content){
            return content.replace(/\r/g,"</br>");
        }
    }


    //拖拽排版
    var dataTemplate = [
        {code:0,select:false,content:"***********************************</br>",templateContent:"<center>********************************</center>"},
        {code:1,select:false,content:"店铺订单编号:44578797879789</br>支付方式:微信支付</br>交易时间:2018-06-10 12:00:00",templateContent:'店铺订单编号:shopOrderNumber \r支付方式:payWay \r支付时间:payTime \r'},
        {code:2,select:false,content:"<table class='table font-bold h6'><tr><td>商品详情</td><td>数量</td><td>金额(元)</td></tr><tr><td>剁椒鱼头</td><td>5</td><td>2.00</td></tr><tr><td>东坡肘子</td><td>1</td><td>25.25</td></tr></table>",templateContent:'<table><tr><td>商品详情</td><td>数量</td><td>金额(元)</td></tr></table>\r'},
        {code:3,select:false,content:"订单原价:260.88 元</br>优惠金额:10.00元</br>会员卡折扣:10.00元</br>实付:<b class='font-bold h4'>250.88元</b>",templateContent:"订单原价:realMoney元 \r优惠金额:couponsMoney元\r会员卡折扣:privilegeMoney元 \r实付:@@2<FS><FB>payMoney元</FB></FS>\r"},
        {code:4,select:false,content:"买家留言:</br>用餐餐具:25人,口味:鸳鸯火锅",templateContent:"买家留言:\rremark \r"},
        {code:5,select:false,content:"<div class='inline w-full m-n'><p class='pull-left m-n'>小明</p><p class='pull-right m-n'>13100000000</p></div></br>地址:兰埔富华里金融科技中心",templateContent:"<LR>userName,userPhone</LR>\raddress\r"},
        {code:6,select:false,content:"桌号:5号",templateContent:'桌号:@@2code \r'},
        {code:7,select:false,content:"<div class='inline w-full m-n'><p class='pull-left m-n'>配送费:</p><p class='pull-right m-n'>4.00 元</p></div>",templateContent:'<LR>配送费,postage元</LR>'},
        {code:8,select:false,content:"<div class='inline w-full m-n'><p class='pull-left m-n'>餐盒费:</p><p class='pull-right m-n'>5.20 元</p></div>",templateContent:'<LR>餐盒费,foodBoxCharge元</LR>'},
    ];

    $scope.addData=function(start,obj){
        var newObj=new Object();
        angular.copy(obj,newObj);
        $scope.data.splice(start,0,newObj);
    }


    $scope.openAddBox=function(item,$event,type){
        for(var i=0;i<$scope.data.length;i++){
            $scope.data[i].select=false;
        }
        for(var i=0;i<$scope.data.length;i++){
            if($scope.data[i]!=item){
                $scope.data[i].addStatus=false;
            }
        }
        item.addStatus=!item.addStatus;
        $scope.addType=type;
        $event.stopPropagation();
    }

    $scope.addType=1;
    $scope.addBox=function(item,type){
        if($scope.data.length>15){
            alert("请勿添加过多重复数据");
            return;
        }

        var index=$scope.data.indexOf(item);
        var start=index+$scope.addType;
        $scope.addData(start,dataTemplate[type]);
    }
}]);
